<?php

namespace Home\Controller;

/**
 * Description of SynController.class控制器实例
 *
 * @author zhangzhen
 * @date 2016-12-22
 */
class SynController extends ParentController {

    protected function _initialize() {
        parent::switchTimeUtc();
        $this->validateUtil = new \Org\Util\Validate();
    }
	/**
     * 获取检查master登录信息
     * @modify zhangzhen 2016-12-22    创建
     * @modify wenhui 2017-05-04 增加国际手机检验
     */
	 public function checkMaster(){
		$mobile = I('post.mobile', '', 'string');
        $password = I('post.password', '', 'string');
        $areaCode = I('post.areaCode', '86', 'string');
        //检查参数
        $this->ajaxValidateReturn($this->validateUtil, $mobile, 'globalMobile',
                    array('globalMobile' => array('function', 'checkGlobalMobile,' . $areaCode, '403,130,error'),
                ));

		Vendor('phpRPC.phprpc_client');
        $client = new \PHPRPC_Client(C('MANAGE_SYN_URL'));
        $client->setEncryptMode(3);
        $sendData = array(
            'flag' => 'uclbrt',
            'time' => time(),
            'mobile' => $mobile,
            'areaCode' => $areaCode,
            'password' => $password
        );
        $clientData = $client->Login($sendData);
		if($clientData == 100){
			returnJson(403, 100);
		}
		if($clientData == 101){
			returnJson(403, 101);
		}
		session('master_id', $clientData['id']);
		returnJson(200, 200, 'success');
	}
	/**
     * 添加客栈
     * @modify zhangzhen 2016-12-22 创建
     * @modify wenhui 2017-05-21 添加时区
     */
    public function addSynHotel() {
        $param = I('post.');

        $validateRule = array(
            'hotel' => 'notnull,notempty,nameLength20',
            'city' => 'notnull,notempty,id',
            'district' => 'notnull,notempty,id',
			'masterentityId' => 'notnull,notempty,id',
            'communityNo' => 'notnull,notempty',
            'universalTime' => 'notnull,notempty',
			'currencyId' => 'notnull,notempty,id',
        );
        $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);

        $name = $param['hotel'];
        $cityId = $param['city'];
        $districtId = $param['district'];
		$communityNo = $param['communityNo'];
        $masterentityId = $param['masterentityId'];
        $universalTime = $param['universalTime'];
		$currencyId = $param['currencyId'];
        //$synLogic = A('Common/Syn', 'Logic');
        // 设置时区
        Vendor('phpRPC.phprpc_client');
        $client = new \PHPRPC_Client(C('MANAGE_SYN_URL'));
        $client->setEncryptMode(3);
        $sendData = array(
            'flag' => 'uclbrt',
            'time' => time(),
            'universalTime' => $universalTime,
        );
        $clientData = $client->getUniversalTime($sendData);
        $universalTimeEtc = isset($clientData) ? $clientData : 'Etc/GMT-8';
        date_default_timezone_set($universalTimeEtc);

        //$addResult = $synLogic->addInfo($name, $cityId, $districtId, $communityNo, $masterentityId, $universalTime);
        $hotelLogic = A('Common/Hotel', 'Logic');
        $addResult = $hotelLogic->addInfo($name, $cityId, $districtId, $universalTime, $communityNo, $masterentityId, $currencyId);
        returnJson(200, 200, $masterentityId);
    }
    /**
     * 添加房型
     * @modify zhangzhen 2016-12-22 创建
     */
	public function addSynRoomType() {
		$param = I('post.');

        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'name' => 'notnull,notempty,nameLength15',
            'price' => 'notnull,notempty,decimal8_2',
        );
        $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);
		$hotelId = $param['hotel'];
        $roomTypeName = $param['name'];
        $roomTypePrice = $param['price'];
		$roomInfos = $param['roomInfo'];
		$roomName = $param['roomName'];
		$count = count($roomName);
		if(!$count){
			returnJson(200, 500, '房间不能为空.');
		}
        // 检查是否是义卖客栈 并且房间不能超过5间
        $result = $this->checkUsageHotel($hotelId);
        if ($result['isFreeHotel'] === true) {
            $this->ajaxValidateReturn($this->validateUtil, $param['charityPrice'], 'notnull,notempty,charityDecimal8_2');
        }

        // 义卖价小于房价
        if (isset($param['charityPrice']) && $param['charityPrice'] >= $param['price']) {
            returnJson(403, 425);
        }

        if ($result['isFreeHotel'] === true && $result['existsRooms'] + $count > MAXROOMS) {
            returnJson(403, 421, '义卖客栈房间不能超过5间'); // 义卖客栈房间不能超过5间
        }

        // 检查客栈是否有房间数量限制
        $checkResult = $this->checkHotelRoomLimit($hotelId, $count);
        if ($checkResult === true) {
            returnJson(403, 318);
        }
       
        $roomInfoModel = D('RoomInfo');
		for($i = 0; $i<$count-1; $i++){
			for($j = $i + 1; $j<count($roomName); $j++){
				if($roomName[$i] === $roomName[$j]){
					returnJson(200, 500, $roomName[$i].'重复,请先修改或暂时不导入该房间.');
				}
			}
			$result = $roomInfoModel->getInfoByName($hotelId,$roomName[$i]);
			if($result){
				returnJson(200, 501, $roomName[$i].'重复,您已经拥有该房间.');
			}
		}

        $charityPrice = isset($param['charityPrice']) ? $param['charityPrice'] : null;

		$synLogic = A('Common/Syn', 'Logic');
		$synLogic->addSynRoomTypeAndRooms($hotelId, $roomTypeName, $roomTypePrice, $roomInfos, $charityPrice);
		returnJson(200, 200);
	}
    /**
     * 添加子账号
     * @modify zhangzhen 2016-12-24 创建
     */
    public function addSynSubs(){
        $userId = getUserId();
        $params = I('post.');
        $hotelId = $params['hotel']; 
        $accounts = $params['account'];
        //循环添加子账号
        $synLogic = A('Common/Syn', 'Logic');
        for($i = 0; $i < count($accounts); $i++){
            $account = explode(',',$accounts[$i]);
            $synLogic->addSynSub($hotelId, $userId, $account[0], $account[1], $account[2], $account[3], $account[4]);
        }
        returnJson(200, 200);
    }
    /**
     * 删除子账号
     * @modify zhangzhen 2016-12-24 创建
     */
    public function deleteSub(){
        $userId = getUserId();
        $params = I('post.');
        $hotelId = $params['hotel']; 
        $account = $params['account'];
        $synLogic = A('Common/Syn', 'Logic');
        $synLogic->deleteSub($hotelId, $userId, $account);
    
        returnJson(200, 200);
    }
	/**
     * 添加房间
     * @modify zhangzhen 2016-12-27 创建
     */
    public function addSynRooms(){
        $userId = getUserId();
        $params = I('post.');
		$roomInfoModel = D('RoomInfo');
        $hotelId = $params['hotel']; 
        $roomInfos = $params['roomInfo'];
		$roomTypeId = $params['roomType'];
		$roomName = $params['roomName'];

        // 检查是否是义卖客栈 并且房间不能超过5间
        $result = $this->checkUsageHotel($hotelId);
        $roomsCount = count($roomName);
        if ($result['isFreeHotel'] === true && $result['existsRooms'] + $roomsCount > MAXROOMS) {
            returnJson(403, 421, '义卖客栈房间不能超过5间'); // 义卖客栈房间不能超过5间
        }

        // 检查客栈是否有房间数量限制
        $checkResult = $this->checkHotelRoomLimit($hotelId, $roomsCount);
        if ($checkResult === true) {
            returnJson(403, 318);
        }

		for($i = 0; $i<count($roomName)-1; $i++){
			for($j = $i + 1; $j<count($roomName); $j++){
				if($roomName[$i] === $roomName[$j]){
					returnJson(200, 500, $roomName[$i].'重复,请先修改或暂时不导入该房间.');
				}
			}
			$result = $roomInfoModel->getInfoByName($hotelId,$roomName[$i]);
			if($result){
				returnJson(200, 501, $roomName[$i].'重复,您已经拥有该房间.');
			}
		}
        //循环添加房间
        $synLogic = A('Common/Syn', 'Logic');
		$synLogic->addSynRooms($hotelId,$roomTypeId,$roomInfos);
        returnJson(200, 200);
    }
	/**
     * 删除房间
     * @modify zhangzhen 2016-12-27 创建
     */
	public function deleteSynRoom(){
		$params = I('post.');
		$hotelId = $params['hotel']; 
        $roomId = $params['roomId'];
		$synLogic = A('Common/Syn', 'Logic');
		$synLogic->deleteSynRoom($hotelId, $roomId);
		returnJson(200, 200);
	}
	/**
     * 获取客栈同步信息
     * @modify zhangzhen 2016-12-27 创建
     */
	public function getSynDetail(){
		$params = I('post.');
		$hotelId = $params['hotel']; 
		$adminSynModel = D('AdminSyn');
		$adminSynDetail = $adminSynModel->getInfoByHotelId($hotelId);
		//获取房间同步信息
		$roomInfoModel = D('RoomInfo');
		$roomInfoDetail = $roomInfoModel->getInfoByHotel($hotelId,[1,2,3,4]);
		foreach($roomInfoDetail as $oneRoomInfo){
			
			$rooms .= ' '.$oneRoomInfo['name'].' ';
		}
		returnJson(200, 200,array(
			'accounts' => $adminSynDetail,
			'rooms' => $rooms,
		));
	}
}
